home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10806 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  53 lines

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Output to file vs. cout
  5. Date: Sun, 10 Mar 1996 17:27:20 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4hv3cf$e93@news.halcyon.com>
  8. References: <JL.96Mar7165152@thyme.id.dth.dk>
  9. NNTP-Posting-Host: blv-pm10-ip17.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. jl@id.dth.dk (J°rn Lind-Nielsen) wrote:
  13.  
  14.  
  15. >Here's another of the probs that comes from porting C to C++:
  16.  
  17. >- I want to be able to redirect program output to either a file or cout.
  18. >  Typically this would be selected by a commandline option ("-o outfile").
  19.  
  20. >- In C I would do something like this:
  21.  
  22.  
  23. >     main()
  24. >     {
  25. >       FILE *outputfile;
  26.  
  27. >       if (commandline == do_output_to_file)
  28. >          outputfile = stdout;
  29. >       else
  30. >          outputfile = fopen(passed_filename, "w");
  31.  
  32. >       fprintf(outfile, "Hello world\n");
  33.  
  34. >       fclose(outfile);  /* Maybe - not allways necassary */
  35. >     }
  36.  
  37.  
  38.  
  39. >- The question is:  How is this done in C++ ?
  40.  
  41. Of course, it could be done exactly the same way.
  42.  
  43. The iostream classes, btw, don't particularly care what the output or
  44. input device really is.  cout and cin are just streams that already
  45. point to the console.  You can make file or memory archives that
  46. conform to the ostream and istream interfaces and substitute them at
  47. will.
  48.  
  49.     archive << "My name: " << szName << "\n";    // etc.
  50.  
  51.                     --Norm 
  52.  
  53.